{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# Copyright 2025 Apache Software Foundation\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ],
   "id": "62688b26eff3080f"
  },
  {
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "!pip install burr[start]",
   "id": "500d692cd72c67b3"
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:11:36.093767Z",
     "start_time": "2024-03-27T00:11:36.088301Z"
    },
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "from typing import List, Optional, Tuple\n",
    "\n",
    "import burr.core\n",
    "from burr.core import Result, State, default, expr\n",
    "from burr.core.action import action\n",
    "from burr.core.persistence import SQLLitePersister\n",
    "from burr.lifecycle import LifecycleAdapter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "64c67b15363653e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:11:36.548223Z",
     "start_time": "2024-03-27T00:11:36.539358Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "@action(reads=[\"counter\"], writes=[\"counter\"])\n",
    "def counter(state: State) -> Tuple[dict, State]:\n",
    "    result = {\"counter\": state[\"counter\"] + 1}\n",
    "    print(f\"counted to {result['counter']}\")\n",
    "    return result, state.update(**result)\n",
    "\n",
    "\n",
    "# some configuration\n",
    "count_up_to: int = 10\n",
    "partition_key: str = \"demo-user\"\n",
    "app_id: Optional[str] = \"some-unique-id\"\n",
    "storage_dir: Optional[str] = \".\"\n",
    "hooks: Optional[List[LifecycleAdapter]] = None\n",
    "# set up a persister -- if you rerun this script, it will resume from where it left off.\n",
    "# change the app_id to start from scratch\n",
    "persister = SQLLitePersister(\"demos.db\", \"counter\")\n",
    "persister.initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f43d7208a78b363e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:11:37.753610Z",
     "start_time": "2024-03-27T00:11:37.490638Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"152pt\" height=\"111pt\"\n",
       " viewBox=\"0.00 0.00 152.25 111.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 107)\">\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-107 148.25,-107 148.25,4 -4,4\"/>\n",
       "<!-- counter -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>counter</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M44.5,-103C44.5,-103 12,-103 12,-103 6,-103 0,-97 0,-91 0,-91 0,-79 0,-79 0,-73 6,-67 12,-67 12,-67 44.5,-67 44.5,-67 50.5,-67 56.5,-73 56.5,-79 56.5,-79 56.5,-91 56.5,-91 56.5,-97 50.5,-103 44.5,-103\"/>\n",
       "<text text-anchor=\"middle\" x=\"28.25\" y=\"-79.95\" font-family=\"Times,serif\" font-size=\"14.00\">counter</text>\n",
       "</g>\n",
       "<!-- counter&#45;&gt;counter -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>counter&#45;&gt;counter</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M56.7,-91.28C66.55,-91.35 74.5,-89.25 74.5,-85 74.5,-82.61 71.98,-80.9 68.03,-79.87\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"68.51,-76.4 58.21,-78.87 67.8,-83.37 68.51,-76.4\"/>\n",
       "<text text-anchor=\"middle\" x=\"109.38\" y=\"-79.95\" font-family=\"Times,serif\" font-size=\"14.00\">counter &lt; 10</text>\n",
       "</g>\n",
       "<!-- result -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>result</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M43.25,-36C43.25,-36 13.25,-36 13.25,-36 7.25,-36 1.25,-30 1.25,-24 1.25,-24 1.25,-12 1.25,-12 1.25,-6 7.25,0 13.25,0 13.25,0 43.25,0 43.25,0 49.25,0 55.25,-6 55.25,-12 55.25,-12 55.25,-24 55.25,-24 55.25,-30 49.25,-36 43.25,-36\"/>\n",
       "<text text-anchor=\"middle\" x=\"28.25\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">result</text>\n",
       "</g>\n",
       "<!-- counter&#45;&gt;result -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>counter&#45;&gt;result</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M28.25,-66.58C28.25,-60.75 28.25,-54.09 28.25,-47.64\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"31.75,-47.98 28.25,-37.98 24.75,-47.98 31.75,-47.98\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x1083289d0>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app = (\n",
    "    burr.core.ApplicationBuilder()\n",
    "    .with_actions(counter=counter, result=Result(\"counter\"))\n",
    "    .with_transitions(\n",
    "        (\"counter\", \"counter\", expr(f\"counter < {count_up_to}\")),\n",
    "        (\"counter\", \"result\", default),\n",
    "    )\n",
    "    .with_identifiers(partition_key=partition_key, app_id=app_id)\n",
    "    .initialize_from(\n",
    "        persister,\n",
    "        resume_at_next_action=True,\n",
    "        default_state={\"counter\": 0},\n",
    "        default_entrypoint=\"counter\",\n",
    "    )\n",
    "    .with_state_persister(persister)\n",
    "    .with_tracker(project=\"demo_counter\", params={\"storage_dir\": storage_dir})\n",
    "    .with_hooks(*hooks if hooks else [])\n",
    "    .build()\n",
    ")\n",
    "app.visualize(include_conditions=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a63802fa10f73ca8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:11:38.483852Z",
     "start_time": "2024-03-27T00:11:38.463109Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "counted to 1\n",
      "counted to 2\n",
      "counted to 3\n",
      "counted to 4\n",
      "counted to 5\n",
      "counted to 6\n",
      "counted to 7\n",
      "counted to 8\n",
      "counted to 9\n",
      "counted to 10\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "action_obj, result, state = app.run(halt_after=[\"result\"])\n",
    "print(state[\"counter\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "42ba7f71fd68f013",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:11:41.692154Z",
     "start_time": "2024-03-27T00:11:41.687049Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'counter': 10, '__SEQUENCE_ID': 10, '__PRIOR_STEP': 'result'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app.state.get_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6b6f52d6ebe9eb2",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
